package com.activitys;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;
import androidx.annotation.NonNull;
import com.applications.BaseApplication;
import com.dialogutils.ToastUtil;
import com.systemutils.PermissionsUtils;
import org.jetbrains.annotations.NotNull;

import java.util.List;


public class BaseActivity extends ActionBarActivity {
    protected Activity activity;
    private long firstTime = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.activity = this;
    }

    /**
     * 请求权限
     *
     * @param permissions
     * @param resule
     */
    public void requestPermissions(String[] permissions, PermissionsUtils.IPermissionsResult resule) {
        PermissionsUtils.getInstance().applyPermissions(this, permissions, resule);
    }

    /**
     * 权限申请回调
     *
     * @param requestCode
     * @param permissions
     * @param grantResults
     */
    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull @NotNull String[] permissions, @NonNull @NotNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        PermissionsUtils.getInstance().onRequestPermissionsResult(this, requestCode, permissions, grantResults);
    }

    /**
     * 检查权限是否获取
     *
     * @param permissions
     * @return
     */
    public boolean onCheckPermiss(String[] permissions) {
        final boolean[] mStatus = {false};
        PermissionsUtils.getInstance().checkPermissions(this, permissions, new PermissionsUtils.IPermissionsResult() {
            @Override
            public void passPermissions() {
                mStatus[0] = true;
            }

            @Override
            public void forbidPermissions() {
                mStatus[0] = false;
                requestPermissions(permissions, null);
            }
        });
        return mStatus[0];
    }

    /**
     * 双击退出
     *
     * @param keyCode
     * @param event
     * @return
     */
    public boolean doubleClickExit(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
            long secondTime = System.currentTimeMillis();
            if (secondTime - firstTime > 1500) {
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                firstTime = secondTime;
                return true;
            } else {
                finish();
            }
        }
        return false;
    }

    /**
     * 退出所有Activity
     */
    public void exitAllActivity(Class keepActivity) {
        List<Activity> mActivityList = ((BaseApplication) getApplicationContext()).getActivityLife();
        for (int i = 0; i < mActivityList.size(); i++) {
            if (mActivityList.get(i).getClass() != keepActivity) {
                mActivityList.get(i).finish();
            }
        }
    }

    /**
     * Toast Utils
     *
     * @param value
     */
    public void toastShow(String value) {
        ToastUtil.show(this, value, Toast.LENGTH_SHORT);
    }
}